package basketball

import (
	"longmen/server/config/global"
	basketball_behaviour2 "longmen/server/internal/services/nami/api/v5/sports/basketball/basketball_behaviour"
	"longmen/server/internal/services/nami/config"
	"longmen/server/internal/services/nami/services/sports/basketball"
	"longmen/server/internal/services/nami/utils/restyclient"
)

type InitBasketBall struct {
}

func (f InitBasketBall) SyncTeamById() {
	id := 0
	for {
		resp := basketball_behaviour2.TeamApiResponse{}
		queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
			"id":    id,
			"limit": 1000,
		})
		err := restyclient.GetUrlAndBindRecords(config.BBTeamsUrl+queryStr, &resp)
		if err != nil {
			global.Log.Error("获取篮球队伍失败: ", err)
			return
		}
		// 空数据
		if len(resp.Results) == 0 {
			global.Log.Info("篮球队伍数据已更新完毕 ")
			break
		}
		if id == resp.Query.MaxID {
			global.Log.Info("篮球队伍数据已刷新完成 ")
			return
		}
		last := resp.Results[len(resp.Results)-1]
		id = last.ID
		// 调用入库
		basketball.CrateTeam(resp.Interface())
	}
}

func (f InitBasketBall) SyncCompetitionById() {
	id := 0
	for {
		resp := basketball_behaviour2.CompetitionApiResponse{}
		queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
			"id":    id,
			"limit": 1000,
		})
		err := restyclient.GetUrlAndBindRecords(config.BBCompetitionUrl+queryStr, &resp)
		if err != nil {
			global.Log.Error("获取篮球赛事失败: ", err)
			return
		}
		// 空数据
		if len(resp.Results) == 0 {
			global.Log.Info("篮球赛事数据已更新完毕 ")
			return
		}
		if id == resp.Query.MaxID {
			global.Log.Info("篮球赛事数据已刷新完成 ")
			return
		}
		last := resp.Results[len(resp.Results)-1]
		id = last.ID
		// 调用入库
		basketball.CrateCompetition(resp.Interface())
	}
}
